package eclipse.client;
import javax.media.opengl.*;
import javax.media.opengl.jgloo.*;
import javax.media.opengl.jgloo.jgl2d.*;
import javax.media.opengl.jgloo.jglsh.*;

import eclipse.Constants;
import eclipse.lib.graphics.GLPrimitives;
import eclipse.lib.graphics.GLTextureLoader;
import eclipse.util.*;
/**
 * The GLObject class represents the OpenGL Object and handles all of the OpenGL calls
 */
/*
 * Copyright (c) 2009, TOD Productions All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
 *	- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
 *	- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
 *	-Neither the name of the organization nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */
public final class Renderer implements GLEventListener
{	
	public static int fps = 0;	
	public final void display(GLAutoDrawable drawable) 
	{			
		final GL gl = drawable.getGL();	
		fps ++;
		gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);  
		if(Database.MENUSTATE == eclipse.Constants.MENUSTATE_SPLASHSCREEN)
		{
			drawSplashScreen(gl);		
			//Database.particlePack.reposition(gl);
			Database.particleEngine.render(gl);
		}
		else if(Database.MENUSTATE == eclipse.Constants.MENUSTATE_INGAME)
		{				
			gl.glPushMatrix();
			{		
						
				gl.glTranslatef(Database.CameraX, Database.CameraY, 0f);				
				GameLogic.drawPlayers(gl);		
				GLPrimitives.drawText(gl, "FPS: " + Database.FPS);
									
			}			
			gl.glPopMatrix();	
		}
		else if(Database.MENUSTATE == Constants.MENUSTATE_CHARSELECT)
			drawCharSelect(gl);
		Database.fpsCount++;
	}	
	private static final void drawCharSelect(GL gl)
	{
		gl.glPushMatrix();
			GLPrimitives.drawPlasmaFilter(gl, Constants.SCREENFILTER, Constants.SPLASHFILTER, Database.tBLANK, Database.logoSpin);			
		gl.glPopMatrix();
		Database.logoSpin += 1f;
	}
	private static final void drawSplashScreen(GL gl)
	{
		gl.glPushMatrix();
		{
			GLPrimitives.drawPlasmaFilter(gl, Constants.SCREENFILTER, Constants.SPLASHFILTER, Database.tBLANK, Database.logoSpin);
			Database.logoSpin += 1;
		}
		gl.glPopMatrix();								
	}
	public final void displayChanged(GLAutoDrawable GLDraw, 
			boolean modeChanged, boolean deviceChanged) 
	{
		
	}
	public final void init(GLAutoDrawable GLDraw) 
	{		 	
		
		//initialize the GL object
		final GL gl = GLDraw.getGL();  
	    if(Database.FPS == 1)
		    gl.setSwapInterval(1);
		gl.glShadeModel(GL.GL_SMOOTH);
		gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
		gl.glClearDepth(1.0f);
		gl.glEnable(GL.GL_DEPTH_TEST);
		gl.glEnable(GL.GL_TEXTURE_2D);
				
		gl.glDepthFunc(GL.GL_LEQUAL);
		if(Database.HIGH_RES)
		{
			gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);
			gl.glHint(GL.GL_POLYGON_SMOOTH_HINT, GL.GL_NICEST);
		}
		else
		{
			gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_FASTEST);		
			gl.glHint(GL.GL_POLYGON_SMOOTH_HINT, GL.GL_FASTEST);
		}		
		loadTextures();		
		
		System.out.println("OpenGL is Initialized");		
		
		final JGLTriad tPart = new JGLTriad(1f);
		tPart.setTexture(Database.tBLANK);
		tPart.setColor(JGLColor.WHITE);		
		tPart.compile(gl);
		Database.particleEngine.generateParticle( 
				tPart,
				100, 200,  
				new JGLVector(GLPrimitives.getX(10), GLPrimitives.getY(0), 0f),
				320, new JGLVector(0f, 5f, 0f),
				10, new JGLVector(0f,  1f, 0f),
				0, new JGLVector(0f,   30f, 0f),
				0,  new JGLColor(1f, 1f, 1f), true, 1);
		/*
		Database.particleEngine.generateParticle(
				20, 200, 2f, 
				new JGLVector(GLPrimitives.getX(10), GLPrimitives.getY(0), 0f),
				320, new JGLVector(0f, 3f, 0f),
				10, new JGLVector(0f,  1f, 0f),
				0, new JGLVector(0f,   10f, 0f),
				0, Database.tBLANK, new JGLColor(1f, 1f, 1f), true);
		*/
		/*
		Database.particlePack.generateParticle(
			1000, 200, 2f, 
			new eclipse.lib.Vector(GLPrimitives.getX(10), GLPrimitives.getY(0), 0f),
			320, new eclipse.lib.Vector(0f, 1f, 0f),
			10, new eclipse.lib.Vector(0f,  1f, 0f),
			0, new eclipse.lib.Vector(0f,    5f, 0f),
			0, Database.tBLANK, new JGLColor(0.1f, 0.1f, 0.1f), true, true);
		*/			
	}
	/**
	 * Loads the textures used
	 *
	 */
	public static final void loadTextures()
	{
		try 
		{					
			System.out.println("loaded standard graphics...");
			//Database.tLogo = GLTextureLoader.loadIMG(new ERL("jar://gfx/logo").openStream(), Database.HIGH_RES, null);
			Database.tLogo = GLTextureLoader.loadUnprocessedIMG(new ERL("jar://gfx/logo").openStream(), Database.HIGH_RES, null);
			Database.tBLANK = GLTextureLoader.loadUnprocessedIMG(new ERL("jar://gfx/blank").openStream(), Database.HIGH_RES, null);									
			System.out.println("loading tilesheets...");
			for(int i = 0; i < Database.MAX_TILESETS; i++)
				Database.tTileSheet[i] = GLTextureLoader.loadIMG(new ERL(Database.TILEDATABASE + "tiles" + i).openStream(), Database.HIGH_RES, null);
			Database.tFont = GLTextureLoader.loadIMG(new ERL("jar://gfx/font").openStream(), Database.HIGH_RES, null);
			System.out.println("loading sprites...");
			for(int i = 0; i < Database.MAX_SPRITESETS; i++)
				Database.tSpriteSheet[i] = GLTextureLoader.loadIMG(new ERL("jar://gfx/sprites/sprite" + i).openStream(), Database.HIGH_RES, null);
			
		} 
		catch (Exception e) 
		{
			eclipse.ErrorManager.handleError(e);
		}	
	}
	/**
	 * Builds all of the objects used in multiple areas
	 * @param gl OpenGL object
	 */ 
	public static final void buildLists(GL gl)
	{
		Database.ID_LOGO = gl.glGenLists(1);
		Database.ID_MENU = gl.glGenLists(1);
		//Logo object
		gl.glNewList(Database.ID_LOGO, GL.GL_COMPILE);
			GLPrimitives.drawQuad(gl, Constants.SPLASHRECT, Database.tLogo);		
		gl.glEndList();				
	}
	public final void reshape(GLAutoDrawable drawable, int x, int y, int width, int height)
	{
		@SuppressWarnings("unused")
		final double left, right, bottom, top, zNear, zFar, fov, aspect;
		fov 	= 45d;
		aspect 	= (double) width / (double) height;
		zNear 	= 0.1d;
		zFar  	= 1000.0d;
					
		top 	= Math.tan(fov* Math.PI /360) * zNear;
		bottom 	= -top;
		left 	= aspect * bottom;
		right 	= aspect * top;
		
		final GL gl = drawable.getGL();			
		gl.setSwapInterval(1);
		gl.glViewport(0, 0, width, height);
		gl.glMatrixMode(GL.GL_PROJECTION);
		gl.glLoadIdentity();
//		gl.glFrustum(left, right, bottom, top, zNear, zFar);
		gl.glOrtho(0d, 640d, 480d, 0d, 0d, 1000d);	
		gl.glMatrixMode(GL.GL_MODELVIEW);
		gl.glLoadIdentity();
		
	}
}
